<template>
  <div class="container">
    <div class="background">
      <img class="bgIMG" src="@/assets/img/background.jpg">
    </div>
    <div class="logo-image">
      <img src="@/assets/img/logo.png" />
      <div class="company-name">昊业怡生科技有限公司</div>
    </div>
    <el-row style="margin: auto">
      <div class="loginCard">
        <div class="loginIMG">
          <img src="@/assets/img/login.jpg" class="bgIMG">
        </div>
        <div class="loginContent">
          <div class="login-title">登录 / Login</div>
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form" v-if="isValid">
            <el-select placeholder="请选择" v-model="formData" size="large" @change="handleSelectChange" value-key="name">
              <el-option
                v-for="item in tableData"
                :key="item.name"
                :label="item.name"
                :value="item"
              >
              </el-option>
            </el-select>
            <!-- 按钮区域 -->
            <el-form-item style="margin-top: 13vh">
              <el-button class="loginBtn" type="primary" @click="enter">
                <span style="margin-right: 2vw">进</span>
                <span>入</span>
              </el-button>
            </el-form-item>
          </el-form>
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form" v-else>
            <!-- 用户名 -->
            <el-form-item prop="username">
              <el-input v-model="loginForm.name" prefix-icon="el-icon-s-custom"
                        placeholder="用户名"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" prefix-icon="el-icon-lock"
                        type="password" placeholder="密码"></el-input>
            </el-form-item>

            <!--          <el-select placeholder="请选择" v-model="formData" size="large" @change="handleSelectChange" value-key="id">-->
            <!--            <el-option-->
            <!--              v-for="item in tableData"-->
            <!--              :key="item.id"-->
            <!--              :label="item.name"-->
            <!--              :value="item"-->
            <!--            >-->
            <!--            </el-option>-->
            <!--          </el-select>-->
            <!-- 按钮区域 -->
            <el-form-item>
              <el-button class="loginBtn" type="primary" @click="login">
                <span style="margin-right: 2vw">登</span>
                <span>录</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-row>
  </div>
</template>

<script>
import request from '@/network/request'
// import { baseURL } from '../../config/baseConfig'

export default {
  name: 'index',
  components: { },
  created () {
    this.$store.commit('removeUser')
    const that = this
    document.onkeydown = function (e) {
      e = window.event || e
      if (that.$route.path === '/login' && (e.code === 'Enter' || e.code === 'enter')) {
        // 验证在登录界面和按得键是回车键enter
        that.login()// 登录函数
      }
    }
  },
  data () {
    return {
      // factory data
      tableData: [],
      formLabelWidth: '120px',
      dialogFormVisible: false,
      urlDialogVisible: false,
      formData: {
        // id: 0,
        name: '',
        factory: 'station1'
      },
      currentFactory: 'station1',
      loginForm: {
        name: '',
        password: '',
        factory: ''
      },
      getUrlForm: {
        pageSize: 1000,
        currentPage: 1
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        name: [{
          required: true,
          message: '请输入登录名',
          trigger: 'blur'
        }, {
          min: 3,
          max: 10,
          message: '长度在 3 到 10 个字符',
          trigger: 'blur'
        }],
        // 验证密码是否合法
        password: [{
          required: true,
          message: '请输入登录密码',
          trigger: 'blur'
        }]
      },
      isValid: false
    }
  },
  methods: {
    handleSelectChange () {
      this.currentFactory = this.formData.factory
      console.log('【登录】当前选择的场站', this.currentFactory)
    },
    // 登录提交表单
    login () {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return
        request({
          url: 'user/login0',
          method: 'post',
          params: {
            name: this.loginForm.name,
            password: this.loginForm.password
          }
        }).then(res => {
          if (res.data.data === null) {
            return this.$message.error('用户名或密码错误')
          }
          this.$message.success('登录成功,请选择进入场站')
          this.isValid = true
          this.tableData = res.data.data.factory
          console.log('查看login0返回', this.tableData)
        })
      })
    },
    enter () {
      request({
        url: 'user/login',
        method: 'post',
        params: {
          name: this.loginForm.name,
          password: this.loginForm.password,
          factory: this.currentFactory
        }
      }).then(res => {
        if (res.data.data === null) {
          return this.$message.error('用户名或密码错误')
        }
        console.log('进入场站接口数据', res.data.data)
        this.$message.success('登录成功')
        this.isValid = true
        // 保存token
        const userId = res.data.data.id
        const username = res.data.data.name
        const role = res.data.data.position
        const permissionlv = res.data.data.permissionLevel
        const type = res.data.data.type
        const factoryName = this.formData.name
        this.$store.commit('setUser', { userId, username, role, permissionlv, type, factoryName })

        this.$store.commit('setFactory', this.formData.factory)
        this.$store.commit('setReportUrl', 'http://39.105.29.195:8000/webroot/decision/view/report?viewlet=guofen%252F')
        // 发生页面跳转
        if ((this.$store.getters.getUser.permissionlv).search('生活垃圾') !== -1) {
          this.$router.push({ path: '/householdWaste/facilityRuntime' })
        } else if ((this.$store.getters.getUser.permissionlv).search('黑水') !== -1) {
          if ((this.$store.getters.getUser.permissionlv).search('培育') !== -1) {
            this.$router.push({ path: '/cultivate/hatch_feed' })
          } else if ((this.$store.getters.getUser.permissionlv).search('垃圾处理') !== -1) {
            this.$router.push({ path: '/garbageDisposal/rdFeed' })
          } else if ((this.$store.getters.getUser.permissionlv).search('种虫养殖') !== -1) {
            this.$router.push({ path: '/wormKing/waiting' })
          }
        } else if ((this.$store.getters.getUser.permissionlv).search('四季青') !== -1) {
          if ((this.$store.getters.getUser.permissionlv).search('称重') !== -1) {
            this.$router.push({ path: '/weight/dungRecord' })
          } else if ((this.$store.getters.getUser.permissionlv).search('固液分离') !== -1) {
            this.$router.push({ path: '/solidLiquid/facilityRuntime' })
          } else if ((this.$store.getters.getUser.permissionlv).search('水处理') !== -1) {
            this.$router.push({ path: '/waterDeal/facilityRuntime' })
          } else if ((this.$store.getters.getUser.permissionlv).search('絮凝') !== -1) {
            this.$router.push({ path: '/flocculation/electricityRecord' })
          } else if ((this.$store.getters.getUser.permissionlv).search('堆肥') !== -1) {
            this.$router.push({ path: '/compost/electricityRecord' })
          } else if ((this.$store.getters.getUser.permissionlv).search('除臭') !== -1) {
            this.$router.push({ path: '/greenSeasons/deodorization' })
          } else if ((this.$store.getters.getUser.permissionlv).search('技术系统维修') !== -1) {
            this.$router.push({ path: '/greenSeasons/systemRepair' })
          }
        } else if ((this.$store.getters.getUser.permissionlv).search('超级管理员') !== -1) {
          // this.$router.push({ path: '/factory/home' })
          this.$router.push({ path: '/userManagement/home' })
        } else {
          this.$router.push({ path: '/householdWaste/facilityRuntime' })
        }
      })
    }
  }
}
</script>

<style scoped>
  .logo-image img {
    width: 80px;
    height: 80px;
    top: 8vh;
    left: 30vw;
    position: absolute;
    z-index: 1;
  }
  .container {
    width: 100vw;
    height: 100vh;
  }
  .background {
    width: 100vw;
    height: 100vh;
    background-repeat: no-repeat;
    filter: blur(8px);
    z-index: -1;
    position: absolute;
  }
  .bgIMG {
    width: 100%;
    height: 100%;
  }
  .loginCard {
    width: 66vw;
    height: 60vh;
    display: flex;
    background-color: #e5e9f2;
    margin: auto;
    border-radius: 20px;
    z-index: 1;
  }
  .loginIMG {
    width: 50%;
    height: 100%;
  }
  .loginContent {
    width: 50%;
    text-align: center;
  }
  .login-title {
    line-height: 20vh;
    font-size: 1.8em;
    color: #e5e9f2;
  }
  >>> .el-input__inner{
    width: 19vw;
    height: 6vh;
    border-radius: 3vh;
    margin-bottom: 2vh;
    padding-left: 2.4vw;
  }
  .el-input {
    font-size: 1.2em;
  }
  >>> .el-input__prefix {
    margin-left: 7.4vw;
  }
  >>> .el-input__icon {
    margin-top: -0.75vh;
  }
  >>> .el-form-item__error {
    margin-top: -1.8vh;
    margin-left: 9vw;
    font-size: 1em;
  }
  .loginBtn {
    width: 19vw;
    height: 6vh;
    border-radius: 3vh;
    background-color: RGB(30,52,91);
    font-size: 1.375em;
  }
  .company-name {
    font-size: 8vh;
    color: #e5e9f2;
    margin-top: 8vh;
    margin-left: 40vw;
    position: absolute;
  }
</style>
